/**
 * Created by joy on 03/09/2017.
 */
import BitcoinCashWalletHelper from '../../common/helpers/chain/BitcoinCashWalletHelper'
import CommonUtil from '../../common/utils/CommonUtil'
import prompt from 'prompt'

export default async () => {
  const bitcoinCashWalletHelper = new BitcoinCashWalletHelper()
  const schema = {
    properties: {
      txid: {
        description: 'Enter txid',
        type: 'string',
        required: true
      },
      index: {
        description: 'Enter index',
        type: 'integer',
        required: true
      },
      balance: {
        description: 'Enter balance',
        type: 'number',
        required: true
      },
      targetAddress: {
        description: 'Enter targetAddress',
        type: 'string',
        required: true
      },
      privateKey: {
        description: 'Enter private key for address',
        type: 'string',
        required: true
      },
      outputMinAmount: {
        description: 'Enter min amount for output',
        type: 'number',
        required: true,
        default: 1
      },
      outputMaxNum: {
        description: 'Enter max num for output',
        type: 'integer',
        required: true,
        default: 30
      },
      fee: {
        description: 'Enter network fee',
        type: 'number',
        required: true,
        default: 0.005
      },
      network: {
        description: 'Enter network type',
        type: 'string',
        required: true,
        default: 'bitcoin'
      },
    }
  }

  const args = await CommonUtil.getArgsFromConsole(prompt, schema)
  const { txid, index, balance, targetAddress, privateKey, outputMinAmount, outputMaxNum, fee, network } = args
  const result = await bitcoinCashWalletHelper.buildAndDistributeTx(
    [{
      utxoTxId: txid,
      utxoOutputIndex: index,
      utxoBalance: bitcoinCashWalletHelper.toSatoshi(balance),
      privateKey: privateKey
    }],
    targetAddress,
    bitcoinCashWalletHelper.toSatoshi(outputMinAmount),
    outputMaxNum - 1,
    bitcoinCashWalletHelper.toSatoshi(fee),
    network
  )
  console.log(result)
}
